<?php
namespace app\api\controller;

use think\Controller;
use think\Cache;
use think\Db;
use app\api\model\Member as MemberModel;
class Mail extends Controller
{
    //通用验证码发送邮件
    public function email()
    {
        if (!is_email(input('mail'))) {
            $this->error('邮箱格式错误');
        }
        $toemail = input('mail');
        $code = mt_rand(100000, 999999);
        session("code", $code);
        //邮箱
        session("codemails", input('mail'));
        $subject = '【' . cms('title') . '】验证码:' . $code;
        $content = '您的验证码:' . $code . '<br>您正在效验' . cms('title') . '邮箱验证,请5分钟内完成本次验证。如非本人操作,请忽略本邮件。';
        if (send_mail($toemail, $subject, $content)) {
            return json(array('code' => 200, 'msg' => '发送成功'));
        } else {
            return json(array('code' => 200, 'msg' => '发送失败'));
        }
    }
    //找回密码验证码
    public function emailretrieve()
    {
    	$member = new MemberModel();
        if (!is_email(input('mail'))) {
            $this->error('邮箱格式错误');
        }
        $mail = $member->where('usermail', input('mail'))->find();
        if (!$mail) {
                $this->error('邮箱不存在');
            }
        $toemail = input('mail');
        $code = mt_rand(100000, 999999);
        //验证码
        session("coderetrieve", $code);
        //邮箱
        session("codemail", input('mail'));
        $subject = '【' . cms('title') . '】验证码:' . $code;
        $content = '您的验证码:' . $code . '<br>您正在效验' . cms('title') . '邮箱验证,请5分钟内完成本次验证。如非本人操作,请忽略本邮件。';
        if (send_mail($toemail, $subject, $content)) {
            return json(array('code' => 200, 'msg' => '发送成功'));
        } else {
            return json(array('code' => 200, 'msg' => '发送失败'));
        }
    }
}